﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ThuPhap.Controls
{
    public partial class TextControl : UserControl
    {
        
        #region property
        private int align = 2;

        public int Align
        {
            get { return align; }
            set {
                if (value != align)
                {
                    align = value;
                    switch (align)
                    {
                        case 0:
                            this.TextBlockTextControl.TextAlignment = TextAlignment.Center;
                            this.TextBoxTextControl.TextAlignment = TextAlignment.Center;
                            break;
                        case 1:
                            this.TextBlockTextControl.TextAlignment = TextAlignment.Justify;
                            this.TextBoxTextControl.TextAlignment = TextAlignment.Justify;
                            break;
                        case 2:
                            this.TextBlockTextControl.TextAlignment = TextAlignment.Left;
                            this.TextBoxTextControl.TextAlignment = TextAlignment.Left;
                            break;
                        case 3:
                            this.TextBlockTextControl.TextAlignment = TextAlignment.Right;
                            this.TextBoxTextControl.TextAlignment = TextAlignment.Right;
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        private bool isItalic;

        public bool IsItalic
        {
            get { return isItalic; }
            set {
                if (value!=isItalic)
                {
                    isItalic = value;
                    if (isItalic)
                    {
                        this.TextBoxTextControl.FontStyle = FontStyles.Italic;
                        this.TextBlockTextControl.FontStyle = FontStyles.Italic;
                    }
                    else
                    {
                        this.TextBoxTextControl.FontStyle = FontStyles.Normal;
                        this.TextBlockTextControl.FontStyle = FontStyles.Normal;
                    }
                }
                
            }
        }

        private bool isBold = false;

        public bool IsBold
        {
            get { return isBold; }
            set {
                if (value != isBold)
                {
                    isBold = value;
                    if (isBold)
                    {
                        this.TextBlockTextControl.FontWeight = FontWeights.Bold;
                        this.TextBoxTextControl.FontWeight = FontWeights.Bold;
                    }
                    else
                    {
                        this.TextBlockTextControl.FontWeight = FontWeights.Normal;
                        this.TextBoxTextControl.FontWeight = FontWeights.Normal;
                    }
                }
            }
        }


        private bool isSelected = false;

        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                if (isSelected != value)
                {
                    isSelected = value;
                    if (isSelected)
                    {
                        this.TextBlockTextControl.Visibility = Visibility.Collapsed;
                        this.TextBoxTextControl.Visibility = this.ResizeImage.Visibility = Visibility.Visible;
                        this.TextBoxTextControl.SelectionStart = this.TextBoxTextControl.Text.Length;
                    }
                    else
                    {
                        this.TextBlockTextControl.Visibility = Visibility.Visible;
                        this.TextBoxTextControl.Visibility = this.ResizeImage.Visibility = Visibility.Collapsed;
                    }
                };
            }
        }


        private FontFamily fontFamily;
        public FontFamily FontFamily
        {
            get
            {
                return fontFamily;
            }
            set
            {
                if (fontFamily != value)
                {
                    fontFamily = value;
                    this.TextBlockTextControl.FontFamily = value;
                    this.TextBoxTextControl.FontFamily = value;
                    
                }
            }
        }

        private double fontSize;

        public double FontSize
        {
            get { return fontSize; }
            set 
            {
                if (fontSize != value)
                {
                    fontSize = value;
                    this.TextBlockTextControl.FontSize = value;
                    this.TextBoxTextControl.FontSize = value;
                }
            }
        }

        private Brush foreground;

        public Brush Foreground
        {
            get { return foreground; }
            set
            {
                if (foreground != value)
                {
                    foreground = value;
                    this.TextBlockTextControl.Foreground = value;
                    this.TextBoxTextControl.Foreground = value;
                }
            }
        }

        private double width;

        public double Width
        {
            get { return width; }
            set {
                if (width != value)
                {
                    width = value;
                    this.TextBlockTextControl.Width = width - 35;
                    this.TextBoxTextControl.Width = value;
                }
            }
        }

        private double height;

        public double Height
        {
            get { return height; }
            set
            {
                if (height != value)
                {
                    height = value;
                    this.TextBlockTextControl.Height = value;
                    this.TextBoxTextControl.Height = value;
                }
            }
        }

        private string text;

        public string Text
        {
            get { return this.TextBlockTextControl.Text; }
            set {
                if (value != text)
                {
                    text = value;
                    this.TextBoxTextControl.Text = text;
                }
            }
        }


        #endregion

        private string unicodeText = "Text Control";
        public string UnicodeText
        {
            get
            {
                return unicodeText;
            }
            set
            {
                if (unicodeText!=value)
                {
                    unicodeText = value;
                }
            }
        }

        public delegate void DragDeltaGestureDelegate(object sender, Microsoft.Phone.Controls.DragDeltaGestureEventArgs e);
        public event DragDeltaGestureDelegate DragDeltaGestureEvent;

        public TextControl()
        {
            InitializeComponent();
            this.TextBoxTextControl.Text = unicodeText;
            this.TextBoxTextControl.Text = App.ConvertVNCode.UnicodeToVniWindows(this.TextBoxTextControl.Text);
           // this.DoubleTap += new EventHandler<GestureEventArgs>(TextControl_DoubleTap);
           
        }

        public void SetUnicodeText(string UnicodeText)
        {
            this.UnicodeText = UnicodeText;
        }

        private void TextBoxTextControl_KeyUp(object sender, KeyEventArgs e)
        {
            string content = this.TextBoxTextControl.Text;
            

            if (e.Key == Key.Back)
            {
                unicodeText = unicodeText == "" ? "" : unicodeText.Substring(0, unicodeText.Length - 1);
            }
            else if ((e.Key != Key.Unknown) && content != "") 
            {
                unicodeText += content == "" ? "" : content.Last().ToString();
            }

            if (content != string.Empty)
            {
                if (content.Last() == '\n')
                    return;
                var arrayUnicode = unicodeText.Split(' ');
                var arrayVniWindows = content.Split(' ');
                int index = App.Typing;
                string textValue = string.Empty;
                if (index == 2)
                {
                    string lastWord = App.ConvertVNCode.UnicodeToVniWindows(App.ConvertVNCode.VniToUnicode(arrayUnicode[arrayUnicode.Length-1]));
                    arrayVniWindows[arrayVniWindows.Length - 1] = lastWord;
                    this.TextBoxTextControl.Text = string.Join(" ", arrayVniWindows);
                }
                else if (index == 1)
                {
                    //this.TextBoxTextControl.Text = App.ConvertVNCode.UnicodeToVniWindows(App.ConvertVNCode.TelexToUnicode(UnicodeText));
                    string lastWord = App.ConvertVNCode.UnicodeToVniWindows(App.ConvertVNCode.TelexToUnicode(arrayUnicode[arrayUnicode.Length - 1]));
                    arrayVniWindows[arrayVniWindows.Length - 1] = lastWord;
                    this.TextBoxTextControl.Text = string.Join(" ", arrayVniWindows);
                }
                
                
                //this.TextBoxTextControl.Text = App.ConvertVNCode.UnicodeToVniWindows(this.TextBoxTextControl.Text);
                
                //this.ScrollTextBlock.ScrollToVerticalOffset(this.ScrollTextBlock.ScrollableHeight);
                //this.ScrollTextBlock.UpdateLayout();
            }
            else
            {
                this.UnicodeText = "";
                this.TextBoxTextControl.Text = "";
            }
        }

        private void TextBoxTextControl_TextChanged(object sender, TextChangedEventArgs e)
        {
            //this.TextBoxTextControl.Focus();
            
            this.TextBoxTextControl.SelectionStart = this.TextBoxTextControl.Text.Length;
        }

        private void GestureListener_DragStarted(object sender, Microsoft.Phone.Controls.DragStartedGestureEventArgs e)
        {
            this.Width = this.ActualWidth;
            this.Height = this.ActualHeight;
        }

        private void GestureListener_DragDelta(object sender, Microsoft.Phone.Controls.DragDeltaGestureEventArgs e)
        {
            var tempWidth = this.Width + e.HorizontalChange;
            var tempHeight = this.Height + e.VerticalChange;
            this.Width = tempWidth < 50 ? 50 : tempWidth;
            this.Height = tempHeight < 50 ? 50 : tempHeight;
        }

        private void TextBoxGestureListener_DragDelta(object sender, Microsoft.Phone.Controls.DragDeltaGestureEventArgs e)
        {
            if (DragDeltaGestureEvent != null)
            {
                DragDeltaGestureEvent(this, e);
            }
        }

        private void TextBoxTextControl_DoubleTap(object sender, GestureEventArgs e)
        {
            //this.IsSelected = false;
        }

        public delegate void abc(object sender, GestureEventArgs e);
        public event abc DoubleTapTextControlEvent;
        private void ResizeImage_DoubleTap(object sender, GestureEventArgs e)
        {
            Image image = sender as Image;
            
            if (DoubleTapTextControlEvent != null)
            {
                var grid = image.Parent as Grid;
                DoubleTapTextControlEvent((TextControl)(grid.Parent), e);
                
            }

        }

        

        

        //void TextControl_DoubleTap(object sender, GestureEventArgs e)
        //{
        //    //this.TextBlockTextControl.Visibility = this.TextBlockTextControl.Visibility == System.Windows.Visibility.Collapsed ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        //    //this.TextBoxTextControl.Visibility = this.TextBlockTextControl.Visibility == System.Windows.Visibility.Collapsed ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        //    //if (this.TextBoxTextControl.Visibility == System.Windows.Visibility.Visible)
        //    //{
        //    //    //this.TextBoxTextControl.Focus();
        //    //    IsSelected = true;
        //    //    this.TextBoxTextControl.SelectionStart = this.TextBoxTextControl.Text.Length;
        //    //}

        //    //if (this.TextBlockTextControl.Visibility == System.Windows.Visibility.Visible)
        //    //{
        //    //    IsSelected = false;
        //    //}
        //    //this.IsSelected = !this.IsSelected;

        //}
    }
}
